#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   For_Jenkins_2_test.py
@Time    :   2022/08/31 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pickle import FALSE, TRUE
from pytest_assume.plugin import assume
 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


RoutinControl_NRC13 = [('31 01', '7F 31 13'),
                       ('31 01 02', '7F 31 13'),
                       ('31 01 DF', '7F 31 13'),
                       ('31 01 02 03 01', '7F 31 13'),
                       ('31 01 DF 00 01', '7F 31 13'),
                       ('31 01 FF', '7F 31 13'),
                       ('31 01 FF 00 44', '7F 31 13'),
                       ('31 01 02 02', '7F 31 13'),
                       ('31 01 02 02 02 02', '7F 31 13'),
                       ('31 01 DF FF', '7F 31 13'),
                       ('31 01 DF FF 02 02', '7F 31 13'),
                       ('31 01 FF 01 01', '7F 31 13'),
                       ('31 01 03', '7F 31 13'),
                       ('31 01 03 02 01', '7F 31 13'),
                       ('31 01 54', '7F 31 13'),
                       ('31 01 54 DF 01', '7F 31 13'),
                       ('31 02', '7F 31 13'),
                       ('31 02 03', '7F 31 13'),
                       ('31 02 03 02 01', '7F 31 13'),
                       ('31 02 54', '7F 31 13'),
                       ('31 02 54 DF 01', '7F 31 13'),
                       ('31 03 03', '7F 31 13'),
                       ('31 03 03 02 01', '7F 31 13'),
                       ('31 03 54', '7F 31 13'), 
                       ('31', '7F 31 13'),
                       ('31 03 54 DF 01', '7F 31 13')
                       ]
RoutinControl_NRC31_Extended = [   ('31 01 02 02 01 02 03 04', '7F 31 31'),
                                   ('31 01 DF FF 01 02 03 04', '7F 31 31'),
                                   ('31 01 FF 01', '7F 31 31'),
                                   ('31 01 FF 00 44 00 00 00 00 01 02 03 05', '7F 31 31')
                                ]
RoutinControl_NRC31_Programming = [('31 01 02 03', '7F 31 31'),
                                   ('31 01 DF 00', '7F 31 31'),
                                   ('31 01 03 02', '7F 31 31'),
                                   ('31 02 03 02', '7F 31 31'),
                                   ('31 03 03 02', '7F 31 31'),
                                   ('31 01 54 DF', '7F 31 31'),
                                   ('31 02 54 DF', '7F 31 31'),
                                   ('31 03 54 DF', '7F 31 31')
                                   ]
RoutinControl_NRC33_Programming = [('31 01 02 02 01 02 03 04', '7F 31 33'),
                                   ('31 01 DF FF 01 02 03 04', '7F 31 33'),
                                   ('31 01 FF 01', '7F 31 33'),
                                   ('31 01 FF 00 44 00 00 00 00 01 02 03 05', '7F 31 33')
                                   ]
RoutinControl_NRC33_Extended = [   ('31 01 03 02', '7F 31 33'),
                                   ('31 02 03 02', '7F 31 33'),
                                   ('31 03 03 02', '7F 31 33'),
                                   ('31 01 54 DF', '7F 31 33'),
                                   ('31 02 54 DF', '7F 31 33'),
                                   ('31 03 54 DF', '7F 31 33')
                                ]

RoutinControl_NRC7F = [  ('31 01 02 03', '7F 31 7F'),
                         ('31 01 DF 00', '7F 31 7F'),
                         ('31 01 03 02', '7F 31 7F'),
                         ('31 02 03 02', '7F 31 7F'),
                         ('31 03 03 02', '7F 31 7F'),
                         ('31 01 54 DF', '7F 31 7F'),
                         ('31 02 54 DF', '7F 31 7F'),
                         ('31 01 FF 01', '7F 31 7F'),
                         ('31 01 FF 00 44 00 00 00 01', '7F 31 7F'),
                         ('31 01 02 02 44 00 00 00 01', '7F 31 7F'),
                         ('31 01 DF FF 44 00 00 00 01', '7F 31 7F'),
                         ('31 03 54 DF', '7F 31 7F')
                       ]

RoutinControl_NRC12 = [  ('31 05 02 03', '7F 31 12'),
                         ('31 05 DF 00', '7F 31 12'),
                         ('31 07 03 02', '7F 31 12'),
                         ('31 08 03 02', '7F 31 12'),
                         ('31 06 03 02', '7F 31 12'),
                         ('31 04 54 DF', '7F 31 12'),
                         ('31 09 54 DF', '7F 31 12'),
                         ('31 0A FF 01', '7F 31 12')
                       ]

RoutinControl_NRC13_Programming = [('31', '7F 31 13'),
                                   ('31 01', '7F 31 13'),
                                   ('31 01 02', '7F 31 13'),
                                   ('31 01 DF', '7F 31 13'),
                                   ('31 01 02 03 01', '7F 31 13'),
                                   ('31 01 DF 00 01', '7F 31 13'),
                                   ('31 01 FF', '7F 31 13'),
                                   ('31 01 FF 00', '7F 31 13'),
                                   ('31 01 FF 00 44', '7F 31 13'),
                                   ('31 01 02 02', '7F 31 13'),
                                   ('31 01 02 02 02 02', '7F 31 13'),
                                   ('31 01 DF FF', '7F 31 13'),
                                   ('31 01 DF FF 02 02', '7F 31 13'),
                                   ('31 01 FF', '7F 31 13'),
                                   ('31 01 FF 01 01', '7F 31 13'),
                                   ('31 01 03', '7F 31 13'),
                                   ('31 01 03 02 01', '7F 31 13'),
                                   ('31 01 54', '7F 31 13'),
                                   ('31 01 54 DF 01', '7F 31 13'),
                                   ('31 02', '7F 31 13'),
                                   ('31 02 03', '7F 31 13'),
                                   ('31 02 03 02 01', '7F 31 13'),
                                   ('31 02 54', '7F 31 13'),
                                   ('31 02 54 DF 01', '7F 31 13'),
                                   ('31 03 03', '7F 31 13'), ('31', '7F 31 13'),
                                   ('31 03 03 02 01', '7F 31 13'),
                                   ('31 03 54', '7F 31 13'), ('31', '7F 31 13'),
                                   ('31 03 54 DF 01', '7F 31 13')
                                   ]

RoutinControl_NRC24 = [('31 02 03 02', '7F 31 24'),
                       ('31 03 03 02', '7F 31 24'),
                       ('31 02 54 DF', '7F 31 24'),
                       ('31 03 54 DF', '7F 31 24')
                       ]

class Test_RoutineControl_Phy(object):

     @allure.severity('normal')
     @allure.story('Routine conteol test')
     @allure.description('Test positive response of 0x31 RID in extended session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_RoutineControl_DID_extended_Phy(self,action,Driver):      
          iStep = 1
          # check update precondition 更新条件查询 0203
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010203', '7101020300', 3, iStep,True)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101DF00', '7101DF0000', 3, iStep,True)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E340101", "6E3401", 0, iStep, False)

          # service driver alignment 售后校准 0302
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31010302', '71010302', 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31030302', '71030302', 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31020302', '71020302', 0, iStep, False)
          
          # static alignment 静态校准 54DF
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '310154DF', '710154DF', 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '310354DF', '710354DF', 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '310254DF', '710254DF', 0, iStep, False)

          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E340100", "6E3401", 0, iStep, False)

         #     # erase flash memory 擦除内存 FF00
         #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF00440005000000084A96', '7101FF0000', 2, iStep, True)

         # Check Transfer Data Validity 传输序列校验 0202
         #     iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '310102023E28C279', '7101020200', 2, iStep, True)

         
     @allure.severity('normal')
     @allure.story('Routine conteol test')
     @allure.description('Test positive response of 0x31 RID in programming session with physical addressing')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     def test_RoutineControl_DID_programming_Phy(self,action,Driver):
          iStep = 1 
          # Check SW/HW Dependency 软硬件一致性查询 FF01
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3101FF01', '7101FF0100', 2, iStep, True)

     ids = [f'{req}' for req, resp in RoutinControl_NRC13]
     @allure.severity('minor')
     @allure.description('Test NRC13 of 0x31 in programming session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC13,ids=ids)
     def test_RoutineControl_NRC13_Programming_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

     ids = [f'{req}' for req, resp in RoutinControl_NRC13]
     @allure.severity('minor')
     @allure.description('Test NRC13 of 0x31 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req, resp',RoutinControl_NRC13,ids=ids)
     def test_RoutineControl_NRC13_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

     ids = [f'{req}' for req, resp in RoutinControl_NRC31_Programming]
     @allure.severity('minor')
     @allure.description('Test NRC31 of 0x31 in programming session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC31_Programming,ids=ids)
     def test_RoutineControl_NRC31_Programming_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

     ids = [f'{req}' for req, resp in RoutinControl_NRC31_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC31 of 0x31 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC31_Extended,ids=ids)
     def test_RoutineControl_NRC31_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)

     ids = [f'{req}' for req, resp in RoutinControl_NRC33_Programming]
     @allure.severity('minor')
     @allure.description('Test NRC33 of 0x31 in programming session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC33_Programming,ids=ids)
     def test_RoutineControl_NRC33_Programming_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, False)

     ids = [f'{req}' for req, resp in RoutinControl_NRC33_Extended]
     @allure.severity('minor')
     @allure.description('Test NRC33 of 0x31 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC33_Extended,ids=ids)
     def test_RoutineControl_NRC33_Extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

     ids = [f'{req}' for req, resp in RoutinControl_NRC7F]
     @allure.severity('minor')
     @allure.description('Test NRC7F of 0x31 in defalut session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC7F,ids=ids)
     def test_RoutineControl_NRC7F_Default_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)

     ids = [f'{req}' for req, resp in RoutinControl_NRC12]
     @allure.severity('minor')
     @allure.description('Test NRC12 of 0x31 with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC12,ids=ids)
     def test_RoutineControl_NRC12_programming_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)

     @allure.severity('minor')
     @allure.description('Test NRC12 of 0x31 with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC12,ids=ids)
     def test_RoutineControl_NRC12_extended_Phy(self,action,Driver,req,resp):
          iStep = 1
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, True)


     ids = [f'{req}' for req, resp in RoutinControl_NRC24]
     @allure.severity('minor')
     @allure.description('Test NRC24 of 0x31 in extended session with physical addressing')
     @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
     @pytest.mark.parametrize('req,resp',RoutinControl_NRC24,ids=ids)
     def test_RoutineControl_NRC24_Phy(self,action,Driver,req,resp):
          iStep = 1 
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E340101", "6E3401", 3, iStep, True)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
          iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, "2E340100", "6E3401", 3, iStep, True)
     